package com.yun.space.pro.news.action;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.yun.space.base.BaseAction;
import com.yun.space.entity.News;
import com.yun.space.tools.HqlHelper;
import com.yun.space.tools.PageBean;

/**
 * 资讯中心相关的action
 * @author guo
 *
 */
public class NewsAction extends BaseAction<News>{
	private File file;//文件
	private String fileName;//
	private String fileContentType; //文件类型 
	private String savePath;
	/*
	 * 	@guo 
	 *  进入资讯中心编辑页面
	 */
	public String toEditInformation() {
		ActionContext.getContext().getValueStack().set("newsg",newsService.getById(getModel().getId()));
		return "toEditInformation";
	}
	//编辑资讯中心
	public String editInformation() {
		try {
			FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getFileName());
			FileInputStream fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			fis.close();
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		getModel().setPictures(getFileName());
		getModel().setPutTime(new Date());
		newsService.update(getModel());
		HqlHelper hqlHelper = new HqlHelper(News.class, "news");
		PageBean pageBean = newsService.getPageBean(pageNum, 10, hqlHelper);
		//获得所有资讯中心的信息
		ActionContext.getContext().getValueStack().set("pageBean",pageBean);
		return "editInformation";
	}
	/*
	 * 	@guo 
	 *  进入资讯中心添加页面
	 */
	public String toAddInformation() {
		return "toAddInformation";
	}
	/*
	 * 	@guo 
	 *  资讯中心添加
	 */
	public String addInformation() {
		try {
			FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getFileName());
			FileInputStream fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			fis.close();
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		getModel().setPictures(getFileName());
		getModel().setPutTime(new Date());
		newsService.save(getModel());
		HqlHelper hqlHelper = new HqlHelper(News.class, "news");
		PageBean pageBean = newsService.getPageBean(pageNum, 10, hqlHelper);
		//获得所有资讯中心的信息
		ActionContext.getContext().getValueStack().set("pageBean",pageBean);
		return "addInformation";
	}
	//删除资讯
	public String delectInformation() {
		//根据id删除
		newsService.delete(getModel().getId());
		HqlHelper hqlHelper = new HqlHelper(News.class, "news");
		PageBean pageBean = newsService.getPageBean(pageNum, 10, hqlHelper);
		//获得所有资讯中心的信息
		ActionContext.getContext().getValueStack().set("pageBean",pageBean);
		return "delectInformation";
	}
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
    public String getSavePath() throws Exception {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
}
